package cn.shadow.module.scene;

import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.util.concurrent.atomic.AtomicLong;

@Data
public abstract class SceneObj {
    private long id;
    private int x;
    private int y;

    public Long getId() {
        return id;
    }

    protected ViewObj viewObj;

    private static AtomicLong ids = new AtomicLong(1000);

    public SceneObj() {
        this.id = ids.incrementAndGet();
        this.viewObj = new ViewObj(id, 1);
    }

    public void afterAddToScene() {

    }

    private ByteBuf[] sendMeMsgArray;
    private long sendTime;

    /**
     * @param sender       接收者
     * @param ctime        时间
     * @param isEnterScene 是否进入场景
     *                     当时间改变或者为空时才收集数据
     */
    public void doSendMeToHero(HeroViewObj sender, long ctime, boolean isEnterScene) {
        if (this.sendTime != ctime || this.sendMeMsgArray == null) {
            this.sendMeMsgArray = SceneObjMsg(ctime, isEnterScene);
            this.sendTime = ctime;
        }
        System.out.println(String.format("玩家%s信息【坐标(%s,%s)】发送给玩家【%s】",
                getId(), getX(), getY(), sender.getId()));
    /*    for (ByteBuf sceneObjMsg : sendMeMsgArray) {
            sender.sendMessage(sceneObjMsg);
        }*/
    }

    protected abstract ByteBuf[] SceneObjMsg(long ctime, boolean isEnterScene);

}
